package com.xiao.sulingwallet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.sulingpublic.utils.PageUtils;
import com.xiao.sulingpublic.utils.Query;
import com.xiao.sulingpublic.utils.R;
import com.xiao.sulingwallet.dao.PurseDao;
import com.xiao.sulingwallet.entity.PurseEntity;
import com.xiao.sulingwallet.entity.TradeStateEntity;
import com.xiao.sulingwallet.service.PurseService;
import com.xiao.sulingwallet.service.TradeStateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;


@Service("purseService")
public class PurseServiceImpl extends ServiceImpl<PurseDao, PurseEntity> implements PurseService {

    @Autowired
    private TradeStateService tradeStateService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurseEntity> page = this.page(
                new Query<PurseEntity>().getPage(params),
                new QueryWrapper<PurseEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 通过用户号,修改钱包金额
     * @param companyUserName :要从哪个号转出
     * @param userName :要转到这个用户号
     * @param amount :金额
     * @return
     */
    @Transactional
    @Override
    public R updateAmount(String companyUserName, String userName, BigDecimal amount) throws Exception {
        //1.修改 companyUserName用户金额 - amount
        QueryWrapper<PurseEntity> queryWrapper = new QueryWrapper<PurseEntity>().eq("pu_member_id", companyUserName);

        PurseEntity purse = baseMapper.selectOne(queryWrapper);
        BigDecimal subtract = purse.getPuAmount().subtract(amount);

        PurseEntity purseEntity = new PurseEntity();
        purseEntity.setPuAmount(subtract);
        int update = baseMapper.update(purseEntity, queryWrapper);
        if(update < 0){
            throw new Exception("修改失败");
        }

        Date date = new Date();
        //往交易详细表添加交易信息
        TradeStateEntity tradeStateEntity = new TradeStateEntity();
        tradeStateEntity.setTrsAmount(amount);
        tradeStateEntity.setTrsTradeType(0);
        tradeStateEntity.setTrsMemberId(companyUserName);
        tradeStateEntity.setTrsTransferUser(userName);
        tradeStateEntity.setTrsCreateDate(date);
        //保存交易详情数据
        boolean save1 = tradeStateService.save(tradeStateEntity);
        if(!save1){
            throw new Exception("修改失败");
        }

        //2.修改 userName用户金额 + amount
        QueryWrapper<PurseEntity> wrapper = new QueryWrapper<PurseEntity>().eq("pu_member_id", userName);
        //先获取金额
        PurseEntity purseEntity1 = baseMapper.selectOne(wrapper);
        BigDecimal add = purseEntity1.getPuAmount().add(amount);
        //修改金额
        purseEntity1.setPuAmount(add);
        int update1 = baseMapper.update(purseEntity1, wrapper);
        if(update1 < 0){
            throw new Exception("修改失败");
        }


        //往交易详细表添加交易信息
        TradeStateEntity tradeState02 = new TradeStateEntity();
        tradeState02.setTrsAmount(amount);
        tradeState02.setTrsTradeType(1);
        tradeState02.setTrsMemberId(userName);
        tradeState02.setTrsTransferUser(companyUserName);
        tradeState02.setTrsCreateDate(date);
        //保存交易详情数据
        boolean save = tradeStateService.save(tradeState02);
        if(!save){
            throw new Exception("修改失败");
        }


        return R.ok();
    }

}